package springapp.service;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import springapp.dao.ReviewDao;
import springapp.entity.ReviewEntity;

@Service
public class ReviewService {

	@Autowired(required = true)
	private ReviewDao reviewDao;

	@Autowired(required = true)
	private ILocationService locationService;

	@Autowired(required = true)
	private IDesignationService designationService;

	@Transactional
	public ReviewEntity addReview(Integer companyId, String review,
			Integer rating, String gender, Integer experience,
			Integer locationId, Integer designationId, String jobType) {
		ReviewEntity reviewEntity = new ReviewEntity(review, rating,
				gender, experience, locationId, designationId, jobType);
		reviewDao.saveEntity(reviewEntity);
		return reviewEntity;
	}

	@Transactional(readOnly=true)
	public ReviewEntity getReview(int reviewId) {
		return this.reviewDao.getEntity(reviewId);
	}

	public void populateReviews(Collection<ReviewEntity> reviews) {
		for (ReviewEntity reviewEntity : reviews) {
			reviewEntity.setDesignationName(designationService
					.getDesignationName(reviewEntity.getDesignationId()));
			reviewEntity.setLocationName(locationService
					.getLocationName(reviewEntity.getLocationId()));
		}
	}
	
	@Transactional(readOnly=true)
	public ReviewEntity[] getReviewsForCompanyId(Integer companyId){
		List<ReviewEntity> reviews = this.reviewDao.getReviewsForCompanyId(companyId);
		populateReviews(reviews);
		return reviews.toArray(new ReviewEntity[0]);
	}
	
	public int getNumReviews(Integer companyId){
		return this.reviewDao.getNumReviewForCompany(companyId);
	}
}
